package api

import (
	"singo/service"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

// MistakeCreate 新增错误日志
// @Summary 新增错误日志
// @Router /api/v1/mistake/create [post]
// @tags 错误日志相关
// @accept application/json
// @Param name query string true "错误日志名称"
// @Param intro query string false "介绍"
// @Param project_id query string false "关联项目ID"
// @Param organization_id query string false "关联组织ID"
// @Param status query int false "状态"
// @Param priority query int false "优先级"
// @Param created_by query int false "创建人"
// @Param execute_by query int false "执行人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func MistakeCreate(c *gin.Context) {
	var service service.MistakeCreateService
	session := sessions.Default(c)
	userUID := session.Get("user_uid")
	userAgent := c.Request.Header.Get("User-Agent")
	var userID string
	if userUID != nil {
		userID = userUID.(string)
	} else {
		userID = ""
	}

	if err := c.ShouldBind(&service); err == nil {
		res := service.MistakeCreate(userID, userAgent)
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// MistakeList 错误日志列表接口
// @Summary 错误日志列表
// @Router /api/v1/mistakes [get]
// @tags 错误日志相关
// @accept application/json
// @Param user_id query string false "关联用户ID"
// @Param organization_id query string false "关联小组ID"
// @Param project_id query string false "关联项目ID"
// @Param status query int false "状态"
// @Param pageNo query int false "页码"
// @Param pageSize query int false "条数"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func MistakeList(c *gin.Context) {
	service := service.ListMistakeService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.MistakeList()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// MistakeShow 错误日志详情接口
// @Summary 错误日志详情
// @Router /api/v1/mistake/{id} [get]
// @tags 错误日志相关
// @accept application/json
// @Param id path string false "错误日志ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func MistakeShow(c *gin.Context) {
	service := service.MistakeShowService{}
	res := service.MistakeShow(c.Param("id"))
	c.JSON(200, res)
}

// MistakeUpdate 更新错误日志接口
// @Summary 更新错误日志
// @Router /api/v1/mistake/{id} [put]
// @tags 错误日志相关
// @accept application/json
// @Param id path string false "错误日志ID"
// @Param name query string false "错误日志名称"
// @Param intro query string false "错误日志介绍"
// @Param priority query int false "优先级"
// @Param status query int false "状态"
// @Param project_id query string false "关联项目ID"
// @Param organization_id query string false "关联组织ID"
// @Param execute_by query string false "执行人"
// @Param deadline_at query string false "截止日期"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func MistakeUpdate(c *gin.Context) {
	service := service.MistakeUpdateService{}
	if err := c.ShouldBind(&service); err == nil {
		res := service.MistakeUpdate()
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
	}
}

// MistakeDelete 删除错误日志接口
// @Summary 删除错误日志
// @Router /api/v1/mistake/{id} [delete]
// @tags 错误日志相关
// @accept application/json
// @Param id path string false "错误日志ID"
// @Success 200 {string} json "{"status": 0, "data": {}, "msg": ""}"
func MistakeDelete(c *gin.Context) {
	service := service.MistakeDeleteService{}
	res := service.MistakeDelete(c.Param("id"))
	c.JSON(200, res)
}
